package com.yunji.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunji.news.entity.RegistrationEntry;
import com.yunji.news.mapper.RegistrationEntryMapper;
import com.yunji.news.service.RegistrationEntryService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

/**
 * 报名入口服务实现类
 */
@Service
public class RegistrationEntryServiceImpl extends ServiceImpl<RegistrationEntryMapper, RegistrationEntry> implements RegistrationEntryService {
    
    @Override
    public Page<RegistrationEntry> getRegistrationEntryPage(Integer current, Integer size, String schoolName, Integer year, Integer status) {
        Page<RegistrationEntry> page = new Page<>(current, size);
        
        LambdaQueryWrapper<RegistrationEntry> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(schoolName), RegistrationEntry::getSchoolName, schoolName)
                   .eq(year != null, RegistrationEntry::getYear, year)
                   .eq(status != null, RegistrationEntry::getStatus, status)
                   .orderByAsc(RegistrationEntry::getSortOrder)
                   .orderByDesc(RegistrationEntry::getCreatedAt);
        
        return this.page(page, queryWrapper);
    }
    
    @Override
    public boolean save(RegistrationEntry entity) {
        entity.setCreatedAt(LocalDateTime.now());
        entity.setUpdatedAt(LocalDateTime.now());
        return super.save(entity);
    }
    
    @Override
    public boolean updateById(RegistrationEntry entity) {
        entity.setUpdatedAt(LocalDateTime.now());
        return super.updateById(entity);
    }
}